package com.gx.controller;

import com.gx.aspect.annotation.OperationLogger;
import com.gx.domain.SysLogOperation;
import com.gx.page.TableDataInfo;
import com.gx.response.R;
import com.gx.service.api.SysLogOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/logs")
public class LogController extends BaseController {

    @Autowired
    private SysLogOperationService sysLogOperationService;

    @OperationLogger(option = "查看日志列表")
    @GetMapping
    public R list(@RequestParam("creatorName") String creatorName, @RequestParam("requestUri") String requestUri) {
        startPage();
        List<SysLogOperation> sysLogOperations = sysLogOperationService.queryListLike(creatorName, requestUri);
        TableDataInfo dataTable = getDataTable(sysLogOperations);
        return R.ok().put("rows", dataTable.getRows()).put("total", dataTable.getTotal());
    }

    @OperationLogger(option = "删除日志")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        sysLogOperationService.delObject(id);
        return R.ok("日志删除成功");
    }

    @OperationLogger(option = "批量删除日志")
    @DeleteMapping("/batch")
    public R batchDelete(@RequestParam("ids") String ids) {
        String[] split = ids.split(",");
        List<Long> collect = Arrays.stream(ids.split(",")).mapToLong(id -> Long.parseLong(id)).boxed().collect(Collectors.toList());
        sysLogOperationService.batchDelete(collect);
        return R.ok("日志删除成功");
    }
}
